﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using EzDesk.Utility;
using EzDesk.Utility.Log;

namespace EzDesk.Clients.Utility
{

        public class FeedDataModel
        {
            public string Title { get; set; }
            public string Body { get; set; }

            public override string ToString()
            {
                return Title + Body;
            }
        }


    /// @Refactorying with Interface to instead of the class
    /// <summary>
    /// 
    /// </summary>
    public class FeedCollection : List<FeedClientModel>
    {
        private string _xmlData = string.Empty;

        private readonly IList<KeyValuePair<string, string>> _parameters = new List<KeyValuePair<string, string>>();

        public static FeedCollection Create(string xmlData, string targetType = "html")
        {
            var feedCollection = new FeedCollection(xmlData);
            FeedTransformBase feedTransform = FeedCollection.CreateTransform(targetType);
            feedTransform.Transform(feedCollection, feedCollection._parameters.ToArray());

            return feedCollection;
        }

        private static FeedTransformBase CreateTransform(string targetType)
        {
            FeedTransformBase feedTransform;
            switch (targetType)
            {
                case "xaml":
                    feedTransform = new XamlFeedTransform();
                    break;
                case "html":
                    feedTransform = new HtmlFeedTransform();
                    break;
                case "desktop-html":
                    feedTransform = new DesktopHtmlFeedTransform();
                    break;
                default:
                    feedTransform = new HtmlFeedTransform();
                    break;
            }
            return feedTransform;
        }

        public FeedCollection()
        { }

        private FeedCollection(string xmlData)
            : base()
        {
            _xmlData = xmlData;

            try
            {
                XElement xelement = XElement.Parse(xmlData);

                _parameters.Add(new KeyValuePair<string, string>(
                        "appportalurl",
                        xelement.Attribute("appportalurl").Value)
                    );

                xelement
                    .Element("feeditems")
                    .Elements("feeditem")
                    .Foreach(item =>
                    {
                        try
                        {
                            Add(FeedClientModel.BuildFeedModel(item));
                        }
                        catch (Exception exp)
                        {
                            LogEx.ERROR("FeedParse", exp.Message, exp);
                            throw;
                        }
                    });

            }
            catch (Exception exp)
            {
                LogEx.ERROR("FeedParse", exp.Message, exp);
                throw;
            }
        }

        public static FeedCollection Create(IEnumerable<FeedClientModel> feeds)
        {
            var feedCollection = new FeedCollection();
            feedCollection.AddRange(feeds);
            return feedCollection;
        }
    }

}
